from django.db import models
from django.forms import ModelForm
from django import forms
from django.contrib.formtools.preview import FormPreview

SCHOOL_CHOICES = (
  ('Arts & Science','Arts & Science'),
  ('Business','Business'),
  ('None','None'),
  ('Other', 'Other'),
)

DIVISION_CHOICES = (
  ('I','I'),
  ('II','II'),
  ('III','III'),
  ('IV','IV'),
  ('V','V'),
  ('None','None') ,
  ('Other', 'Other'),                   
)

class Election(models.Model):
    electionDescription = models.CharField(max_length=200)
    electionDate = models.DateTimeField()
    electionEndDate = models.DateTimeField()
    def __unicode__(self):
        return self.electionDate.__str__()

class Issue(models.Model):
    election = models.ForeignKey(Election)#each Issue must be part of an election
    issueName = models.CharField(max_length = 200)
    issueDescription = models.CharField(max_length=1000)
    schoolRestriction = models.CharField(max_length=30, choices=SCHOOL_CHOICES)
    divisionRestriction = models.CharField(max_length=20, choices=DIVISION_CHOICES)
    #start_date = models.DateTimeField('date started')
    #end_date = models.DateTimeField('date ended')
    def __unicode__(self):
        return self.issueDescription
      

class Choice(models.Model):
    issue = models.ForeignKey(Issue)#each Choice related to a single poll
    choiceDescription = models.CharField(max_length=200)
    votes = models.PositiveIntegerField(default=0)
    #vote = models.BooleanField
    def __unicode__(self):
        return self.choiceDescription

class ChoiceForm(forms.Form):
    class Meta:
        exclude = ('votes')
        comment = forms.CharField(widget=forms.Textarea)

#class Choice_votes(models.Model):
#    choice = models.ForeignKey(Choice)
#    votes = models.IntegerField()
#    def _unicode_(self):
#        return self.votes
       
class Department(models.Model):
    department = models.CharField(max_length=30, primary_key=True)
    school = models.CharField(max_length=30, choices=SCHOOL_CHOICES)
    division = models.CharField(max_length=20, choices=DIVISION_CHOICES)
    def __unicode__(self):
        return self.department

class DepartmentForm(ModelForm):
    class Meta:
        model = Department

class Position(models.Model):
    election = models.ForeignKey(Election)#each Issue must be part of an election
    posName = models.CharField(max_length=250)
    posDescription = models.CharField(max_length=250)
    schoolRestriction = models.CharField(max_length=30, choices=SCHOOL_CHOICES)
    divisionRestriction = models.CharField(max_length=20, choices=DIVISION_CHOICES)
    maxChoices = models.IntegerField() 
   # votes = models.IntegerField() #need to set the default to zero
    def __unicode__(self):
        return self.posName

class PositionForm(ModelForm):
    class Meta:
        model = Position

#class Position_votes(models.Model):
#    position = models.ForeignKey(Position)
#    votes = models.IntegerField()
#    def _unicode_(self):
#        return self.votes

class FacultyMember(models.Model):
    bannerId = models.CharField(max_length=10, unique=True)
    lastName = models.CharField(max_length=25)
    firstName = models.CharField(max_length=25)
    nickname = models.CharField(max_length=25)
    department = models.ForeignKey(Department)
    def __unicode__(self):
        return u'%s,%s' % (self.lastName, self.firstName)

class PositionForm(ModelForm):
    class Meta:
        model = FacultyMember

class Candidate(models.Model):
    facultyMember = models.ForeignKey(FacultyMember)
    position = models.ForeignKey(Position)
    votes = models.PositiveIntegerField(default=0) #need to set the default to zero
    def __unicode__(self):
        return self.facultyMember.__unicode__() 

class CandidateForm(ModelForm):
    class Meta:
        model = Candidate
        exclude = ('votes')
        
#class MyModelChoiceField(ModelChoiceField):
#    def label_from_instance(self, obj):
#        return "My Object #%i" % obj.id

class VoteForm(forms.Form):
    def __init__(self, *args,**kwrgs): #* - list of positional arguments   ** - dictionary of keyword arguements
        self.issue = kwrgs.pop('issue')#gets value of key 'issue', takes out of dictionary
        super(VoteForm, self).__init__(*args, **kwrgs)#call superclass constructor
        self.fields['choices'] = forms.ModelChoiceField(widget = forms.RadioSelect,queryset=self.issue.choice_set.all(),empty_label=None)#makes new fields
    def getIssueName(self):
        issueName = self.issue.issueName
        return issueName
    def getIssueDecription(self):
        issueDescription = self.issue.issueDescription
        return issueDescription
        
class PositionForm(forms.Form):
    def __init__(self, *args,**kwrgs): #* - list of positional arguments   ** - dictionary of keyword arguements
        self.position = kwrgs.pop('position')#gets value of key 'issue', takes out of dictionary
        super(PositionForm, self).__init__(*args, **kwrgs)#call superclass constructor
        self.fields['candidate'] = forms.ModelMultipleChoiceField(widget = forms.CheckboxSelectMultiple,queryset=self.position.candidate_set.all())#makes new fields
    def getPositionName(self):
        posName = self.position.posName
        return posName
    def getPositionDecription(self):
        posDescription = self.position.posDescription
        return posDescription
    def getMaxChoices(self):
        maxChoices = self.position.maxChoices
        return maxChoices
    def getErrors(self):
        return self.position.errors
    def clean(self):
        clean_data = self.cleaned_data
        candidate = clean_data.get('candidate')
        print candidate
        maxChoices = self.position.maxChoices
        print maxChoices
        if candidate and maxChoices:
            print "Number of candidates"
            print len(candidate)
            if maxChoices < len(candidate):
                raise forms.ValidationError("Too many candidates selected")
        return clean_data
        

                
class UploadDataForm(forms.Form):
    spreadsheet = forms.FileField()
